#include "EquationSolver.h"
using namespace std;
const double l = 89, h = 49, beta1 = 11.5 / 180 * M_PI;
double D1 = 55, D2 = 30;
double A = l * sin(beta1);
double B = l*cos(beta1);
double C1 = (h + 0.5 * D1) * sin(beta1) - 0.5 * D1 * tan(beta1), C2 = (h + 0.5 * D2) * sin(beta1) - 0.5 * D2 * tan(beta1);
double E1 = (h + 0.5 * D1) * cos(beta1) - 0.5 * D1, E2 = (h + 0.5 * D2) * cos(beta1) - 0.5 * D2;

class Function1 : public Function
{
public:
    double operator()(double _x);
    double diff(double _x);
};

double Function1::operator()(double _x){
    double f;
    f = A * sin(_x) * cos(_x) + B * pow(sin(_x), 2) - C1 * cos(_x) - E1 * sin(_x);
    return f;
}

double Function1::diff(double _x){
    double f;
    f = A * (pow(cos(_x), 2) - pow(sin(_x), 2)) + 2 * B * sin(_x) * cos(_x) + C1 * sin(_x) - E1 * cos(_x);
    return f;
}

class Function2 : public Function
{
public:
    double operator()(double _x);
    double diff(double _x);
};

double Function2::operator()(double _x){
    double f;
    f = A * sin(_x) * cos(_x) + B * pow(sin(_x), 2) - C2 * cos(_x) - E2 * sin(_x);
    return f;
}

double Function2::diff(double _x){
    double f;
    f = A * (pow(cos(_x), 2) - pow(sin(_x), 2)) + 2 * B * sin(_x) * cos(_x) + C2 * sin(_x) - E2 * cos(_x);
    return f;
}

int main(int argc,char *argv [ ]){
    cout << "F:" << endl;
    //(a)小问.
    Function1 func1;
    Newton newton1(33 * M_PI / 180, eps, 50, func1);
    double root1 = newton1.solve();
    cout << "(a) solution:" << endl;
    cout << "The obtained angle alpha is: " << root1 * 180 / M_PI << " f(root)=" << func1(root1) << endl;
    //(b)小问.
    Function2 func2;
    Newton newton2(33 * M_PI / 180, eps, 50, func2);
    double root2 = newton2.solve();
    cout << "(b) solution:" << endl;
    cout << "The obtained angle alpha is: " << root2 * 180 / M_PI << " f(root)=" << func2(root2) << endl;
    //(c)小问.
    Secant sec1(32 * M_PI / 180, 34 * M_PI / 180, eps, 20, func1);
    Secant sec2(120 * M_PI / 180, 160 * M_PI / 180, eps, 20, func1);
    double root3 = sec1.solve(), root4 = sec2.solve();
    cout << "(c) solution:" << endl;
    cout << "When the initial value is close to 33: alpha=" << root3 * 180 / M_PI << " f(root)=" << func1(root3) << endl;
    cout << "When the initial value far away from 33: alpha=" << root4 * 180 / M_PI << " f(root)=" << func1(root4) << endl;
    return 0;
}
